package com.woniuxy.portal.controller;

import com.woniuxy.common.consts.UserConst;
import com.woniuxy.portal.model.entity.China;
import com.woniuxy.portal.model.vo.AddressRequest;
import com.woniuxy.portal.model.vo.AddressVo;
import com.woniuxy.portal.service.AddressService;
import com.woniuxy.portal.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin(origins = "*")
@Slf4j
@Api(tags = "收货地址接口")
@RestController
@RequestMapping("/address")
public class AddressController {

    private final AddressService addressService;
    private final JwtUtil jwtUtil;

    @Autowired
    public AddressController(AddressService addressService, JwtUtil jwtUtil) {
        this.addressService = addressService;
        this.jwtUtil = jwtUtil;
    }

    /**
     * 通过用户id获取当前用户的收货地址
     *
     * @param token 用户身份认证令牌
     * @return ResponseEntity<List < AddressVo>> 返回当前用户的收货地址列表
     */
    @GetMapping("/findAddress")
    @ApiOperation("获取当前用户的收货地址")
    public ResponseEntity<List<AddressVo>> findAddress(
            // 可省略required = true，默认值可不写
            @RequestHeader(name = "Authorization") String token) {
        //获取当前登录用户的id
        String userId = jwtUtil.getValue(token, UserConst.USER_ID);
        //获取当前用户的地址列表
        List<AddressVo> list = addressService.findByUserId(Long.parseLong(userId));
        return ResponseEntity.ok(list);
    }

    /**
     * 获取所有省份信息
     *
     * @return ResponseEntity<List < China>> 包含所有省份信息的响应实体，其中body为China对象列表
     */
    @GetMapping("/getAllProvince")
    @ApiOperation("获取所有省份")
    public ResponseEntity<List<China>> getAllProvince() {
        // 调用地址服务获取所有省份数据
        List<China> list = addressService.getAllProvince();
        log.info("获取所有省份: {}", list);
        return ResponseEntity.ok(list);
    }

    /**
     * 根据省份id，查询城市
     *
     * @param provinceId 省份id
     * @return ResponseEntity<List < China>> 城市列表
     */
    @GetMapping("/getAllCities")
    @ApiOperation("根据省份id，查询城市")
//    @ApiImplicitParam(name = "provinceId", value = "省份id", required = true, dataType = "Integer")
    @ApiImplicitParam(name = "provinceId", value = "省份id", required = true, dataTypeClass = Integer.class)
    public ResponseEntity<List<China>> getAllCities(Integer provinceId) {
        log.info("省份id：{}", provinceId);
        // 调用服务获取城市列表
        List<China> list = addressService.getAllCities(provinceId);
        return ResponseEntity.ok(list);
    }

    /**
     * 根据城市id查询区/县信息
     *
     * @param cityId 城市id，用于查询该城市下的所有区/县
     * @return 返回区/县信息列表的响应实体
     */
    @GetMapping("/getAllDistricts")
    @ApiOperation("根据城市id，查询区/县")
//    @ApiImplicitParam(name = "cityId", value = "城市id", required = true, dataType = "Integer")
    @ApiImplicitParam(name = "cityId", value = "城市id", required = true, dataTypeClass = Integer.class)
    public ResponseEntity<List<China>> getAllDistricts(Integer cityId) {
        log.info("城市id：{}", cityId);
        // 调用地址服务获取区/县数据
        List<China> list = addressService.getAllDistricts(cityId);
        return ResponseEntity.ok(list);
    }

    /**
     * 保存地址
     *
     * @param addressRequest 地址信息请求对象，包含要保存的地址详细信息
     * @param token          用户身份认证令牌，用于获取当前登录用户信息
     * @return ResponseEntity<Void> HTTP响应实体，返回200状态码表示保存成功
     */
    @PostMapping("/save")
    @ApiOperation("保存地址")
    public ResponseEntity<Void> save(
            @RequestBody AddressRequest addressRequest, @RequestHeader("Authorization") String token) {
        // 获取当前登录的用户id
        String userId = jwtUtil.getValue(token, "userId");
        addressService.addAddress(addressRequest, Long.valueOf(userId));
        return ResponseEntity.ok().build();
    }

}
